package com.qdxwx.manage.controller.category;

import com.qdxwx.common.entity.Ztree;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.category.CategoryService;
import com.qdxwx.data.service.channel.ChannelService;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.category.Category;
import com.qdxwx.models.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 分类Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/category")
public class CategoryListController extends BaseController {
    private final String prefix = "category";

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ChannelService channelService;

    @Permissions("category:list")
    @GetMapping("/list.html")
    public String categoryList(HttpServletRequest request, ModelMap mmap) {
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        Channel channel = new Channel();
        channel.setChannelType(0);
        channel.setIsDisabled(0);
        List<Channel> channelList = channelService.getList(channel,null);
        if(channelId < 1 && channelList.size() > 0) {
            channelId = channelList.get(0).getChannelId();
        }
        mmap.put("channelId", channelId);
        mmap.put("channelList", channelList);
        return prefix + "/category-list";
    }

    /**
     * 查询分类树列表
     *
     * @return
     */
    @Permissions("category:list")
    @RequestMapping("/list.json")
    @ResponseBody
    public List<Category> list(HttpServletRequest request) {
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        Category category = new Category();
        category.setChannelId(channelId);
        return categoryService.getList(category);
    }

    /**
     * 选择分类树
     */
    @GetMapping(value = "/select.html")
    public String selectCategory(HttpServletRequest request,  ModelMap mmap) {
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        int categoryId = TKConvert.toInteger(request.getParameter("categoryId"));
        mmap.put("channelId", channelId);
        if (categoryId > 0) {
            mmap.put("category", categoryService.get(categoryId));
        }
        return prefix + "/category-tree";
    }

    /**
     * 加载分类树列表
     */
    @GetMapping("/tree.json")
    @ResponseBody
    public List<Ztree> treeData(HttpServletRequest request) {
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        return categoryService.getTree(channelId);
    }
}
